home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir41 / zi.zip / ZI.CPP < prev    next >
C/C++ Source or Header  |  1993-01-14  |  4KB  |  167 lines

  1. //
  2. //   Module: zi.cpp
  3. //  Version: 1.1
  4. //  Release: 101
  5. //     Date: 01/14/93 18:47:41
  6. //
  7. //      ZI check for debug info in object file
  8. //
  9. //     COPYRIGHT (C) 1992.  All Rights Reserved.
  10. //     Baumeister Corporation. Granbury, Texas.
  11. //
  12. // Relaeased to the public domain by
  13. //
  14. //      Louis F. Springer, CDP
  15. //      Baumeister Corporation
  16. //      2A Thunderbird Cove
  17. //      Granbury, Texas 76049
  18. //      (817)326-5329
  19. //      CIS 73770,1137
  20. //
  21. //
  22. // 1.0  01/14/93 LOU        Initial rev.
  23. //                          ------------------------------------------------
  24. // 1.1  01/14/93 LOU        Fixed error in case statement.
  25. //                          ------------------------------------------------
  26. //
  27. // $nokeywords$
  28. //
  29. // Read the object files listed in standard in and output the source
  30. // file name of the ones
  31. // which have debug info on them to stdout.
  32. //
  33. // Example:
  34. //
  35. // dir *.obj /b | zi | awk "{cmd = \"touch \" $1; system(cmd);}"
  36. //
  37. // Will "touch" all files which have debug info in them. Useful to 
  38. // catch everything you've been debugging prior to a production
  39. // complile. 
  40. //
  41.  
  42. #include <string.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <malloc.h> 
  46. #include "omf.h"
  47.  
  48. char *hasLinnum(char *objFile);
  49. int getRec(FILE *in);
  50.  
  51. #define BUFSZ 1024
  52.  
  53. // rec static global used to hold omf records
  54. static omf rec;
  55.  
  56. // remember the source file name in the obj file here
  57. static char sourceFile[BUFSIZ];
  58.  
  59. extern int errno;
  60.  
  61. int main(int argc, char *argv[])
  62. {
  63.     int numlines = 0;
  64.     char objFile[BUFSZ];
  65.     fgets(objFile, BUFSZ-1, stdin);
  66.  
  67.     while(!feof(stdin) && !ferror(stdin))
  68.     {
  69.         numlines++;
  70.  
  71.         // remove '\n'
  72.         char *nl = strchr(objFile, '\n');
  73.         if(nl) *nl='\0';
  74.  
  75.         if(hasLinnum(objFile)) printf("%s\n", sourceFile);
  76.  
  77.         fgets(objFile, BUFSZ-1, stdin);
  78.         }
  79.     return 0;
  80.     }
  81.  
  82.  
  83. // does the object file specified have a LINNUM record?
  84. // return the source file name
  85. char *hasLinnum(char *objFile)
  86. {
  87.     FILE *in = fopen(objFile, "rb");
  88.     rec.data = NULL;    // initialize data
  89.  
  90.     // initalize sourceFile to indicate no name in obj file
  91.     sprintf(sourceFile, "%s NO SOURCE NAME IN OBJ", objFile);
  92.  
  93.     while(getRec(in))
  94.     {
  95.         switch(rec.hdr.type)
  96.         {
  97.         // should be the first reord in the file
  98.         // get the source file name
  99.         case OMF_THEADR:
  100.             {
  101.                 omfTheadr *theadr = (omfTheadr *)rec.data;
  102.                 sprintf(sourceFile, "%*.*s",
  103.                     theadr->strlen, theadr->strlen, theadr->data);
  104.                 }
  105.             break;
  106.  
  107.         case OMF_LINNUM1:
  108.         case OMF_LINNUM2:
  109.             fclose(in);
  110.             return(sourceFile);
  111.             break;
  112.  
  113.             }
  114.  
  115.         }
  116.     fclose(in);
  117.     return NULL;
  118.     }
  119.  
  120. // get the next record from the obj file
  121. int getRec(FILE *in)
  122. {
  123.     if(rec.data) free(rec.data);
  124.  
  125.     int itemsRead = fread(&rec.hdr, sizeof(rec.hdr), 1, in);
  126.  
  127.     if(!itemsRead || feof(in) || ferror(in))
  128.     {
  129.         rec.hdr.type = 0x00;
  130.         rec.hdr.len = 0x00;
  131.         return 0;
  132.         }
  133.  
  134.     rec.data = malloc(rec.hdr.len);
  135.     if(rec.data)
  136.     {
  137.         // read data
  138.         if(rec.hdr.len - 1)
  139.         {
  140.             itemsRead = fread(rec.data, rec.hdr.len - 1, 1, in);
  141.             if(feof(in) || ferror(in))
  142.             {
  143.                 fprintf(stderr, "Error reading file.\n");
  144.                 free(rec.data);
  145.                 return 0;
  146.                 }
  147.             }
  148.  
  149.         // checksum
  150.         itemsRead = fread(&rec.checkSum, sizeof(rec.checkSum), 1, in);
  151.         if(feof(in) || ferror(in))
  152.         {
  153.             fprintf(stderr, "Error reading file.\n");
  154.             free(rec.data);
  155.             return 0;
  156.             }
  157.         return 1;
  158.         }
  159.     else
  160.     {
  161.         fprintf(stderr,
  162.             "Can't allocate %d bytes for buffer for record type 0x%.2xh.\n",
  163.            rec.hdr.len, rec.hdr.type);
  164.         return 0;
  165.         }
  166.     }
  167.